package com.pb.metadata.controller;

import com.pb.metadata.service.DataBaseManagementService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库管理
 * @author liaohaoyuan
 * @date 20210301
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/dataBaseManagement")
public class DataBaseManagementController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private DataBaseManagementService dataBaseManagementService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "param_database";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "数据库管理";

    /**
     * 查询分页数据
     * @param map 查询参数
     * @param page 页码
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> map, Page page) {
        return dataBaseManagementService.select(map, page);
    }

    /**
     * 保存数据库信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        try {
            Result insert = dataBaseManagementService.insert(paramMap, request);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 修改数据库
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap, HttpServletRequest request){
        try {
            Result insert = dataBaseManagementService.update(paramMap, request);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 查询数据库类型下拉列表
     *
     * @return Result 返回数据
     */
    @GetMapping("getDatabaseList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getDatabaseList() {
        return dataBaseManagementService.getDatabaseList();
    }

    /**
     * 删除运行环境数据
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        dataBaseManagementService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 启用停用数据库
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("startOrStop")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result startOrStop(@RequestBody List<Map<String,Object>> paramMap){
        dataBaseManagementService.startOrStop(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 查询数据库用户
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("getDbUserDataList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getDbUserDataList(@RequestBody Map<String, Object> paramMap){
        return dataBaseManagementService.getDbUserDataList(paramMap);
    }

    /**
     * 设置数据库用户
     * @param paramList 请求参数
     * @return Result 返回数据
     */
    @PostMapping("setDbUserData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result setDbUserData(@RequestBody List<Map<String,Object>> paramList){
        Map paraMap=new HashMap();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userId = (String) request.getSession().getAttribute("token");
        paraMap.put("createUser",userId);
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
        paraMap.put("createTime",format.format(new Date()));
        Result insert = dataBaseManagementService.setDbUserData(paramList,paraMap);
        return insert;
    }

    /**
     * 删除数据库用户
     * @param paramList 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delDbUserData")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delDbUserData(@RequestBody List<Map<String,Object>> paramList){
        try {
            Result insert = dataBaseManagementService.delDbUserData(paramList);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

}
